package com.koron.housing.staff.service.impl;


import com.koron.housing.staff.bean.po.HousingLease;
import com.koron.housing.staff.mapper.HousingLeaseMapper;
import com.koron.housing.staff.service.HousingLeaseService;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 租赁信息(HousingLease)表服务实现类
 *
 * @author jzp
 * @since 2022-02-26 12:03:13
 */
@Service("leaseService")
public class HousingLeaseServiceImpl implements HousingLeaseService {

    /**
     * 通过ID查询单条数据
     */
    @Override
    @TaskAnnotation("getById")
    public HousingLease getById(SessionFactory factory, String id) {
        HousingLeaseMapper mapper = factory.getMapper(HousingLeaseMapper.class);
        return mapper.selectById(id);
    }

    /**
     * 查询租户最新的租赁信息
     */
    @TaskAnnotation("getNew")
    public HousingLease getNewByStaffId(SessionFactory factory, String staffId) {
        HousingLeaseMapper mapper = factory.getMapper(HousingLeaseMapper.class);
        return mapper.selectNewByStaffId(staffId);
    }

    /**
     * 查询所有数据
     */
    @Override
    @TaskAnnotation("list")
    public List<HousingLease> list(SessionFactory factory, HousingLease housingLease) {
        HousingLeaseMapper mapper = factory.getMapper(HousingLeaseMapper.class);
        return mapper.selectList(housingLease);
    }

    /**
     * 新增数据
     */
    @Override
    @TaskAnnotation("save")
    public boolean save(SessionFactory factory, HousingLease housingLease) {
        HousingLeaseMapper mapper = factory.getMapper(HousingLeaseMapper.class);
        return mapper.insertOrUpdate(housingLease) > 0;
    }

    @Override
    @TaskAnnotation("saveOrUpdate")
    public boolean saveOrUpdate(SessionFactory factory, HousingLease housingLease) {
        if (StringUtils.isEmpty(housingLease)) {
            return save(factory, housingLease);
        } else {
            return update(factory, housingLease);
        }
    }

    /**
     * 修改数据
     */
    @Override
    @TaskAnnotation("update")
    public boolean update(SessionFactory factory, HousingLease housingLease) {
        HousingLeaseMapper mapper = factory.getMapper(HousingLeaseMapper.class);
        return mapper.update(housingLease) > 0;
    }

    /**
     * 通过主键删除数据
     */
    @Override
    @TaskAnnotation("deleteById")
    public boolean deleteById(SessionFactory factory, String id) {
        HousingLeaseMapper mapper = factory.getMapper(HousingLeaseMapper.class);
        return mapper.hidden(id) > 0;
    }

    /**
     * 保存或更新一条租赁信息
     */
    @Override
    @TaskAnnotation("saveNew")
    public boolean saveNew(SessionFactory factory, HousingLease housingLease) {
        Assert.notNull(housingLease.getStatus(), "租赁信息状态不能为空");
        housingLease.setId(null);
        // 最新一条数据
        HousingLease newLease = getNewByStaffId(factory, housingLease.getStaffId());
        if ("0".equals(housingLease.getStatus())) {
            // 如果是未入住。
            // 如果最新租赁信息不存在或者为已退租，新增；如果为已入住，报错；如果为未入住，更新
            if (newLease == null || "2".equals(newLease.getStatus())) {
                return save(factory, housingLease);
            } else if ("1".equals(newLease.getStatus())) {
                // throw new IllegalArgumentException("最新租赁信息中显示您已入住");
            } else if ("0".equals(newLease.getStatus())) {
                housingLease.setId(newLease.getId());
                return update(factory, housingLease);
            }
        } else if ("1".equals(housingLease.getStatus())) {
            // 如果是已入住。入住时间和住房合同不能未空
            // 如果最新租赁信息为不存在或者为已退租，保存一条
            if (newLease == null || "2".equals(newLease.getStatus())) {
                return save(factory, housingLease);
                // throw new IllegalArgumentException("最新租赁信息中未找到您的申请单");
            } else {
                housingLease.setId(newLease.getId());
                return update(factory, housingLease);
            }
        } else if ("2".equals(housingLease.getStatus())) {
            // 如果是已退租。退租时间不能未空
            // 如果最新租赁信息为不存在或者为未入住，报错
            if (newLease == null || "0".equals(newLease.getStatus())) {
                // throw new IllegalArgumentException("最新租赁信息中未找到您的入住信息");
            } else {
                housingLease.setId(newLease.getId());
                return update(factory, housingLease);
            }
        }
        return false;
    }
}
